package rickyGame.game.action.ai
{
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Morton;
	import ricky.quadtree.Quadtree;
	import ricky.utils.FrameManager;
	
	import rickyGame.model.config.*;
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.action.ActUtils;
	import rickyGame.game.action.MoveLeafAction;
	import rickyGame.game.action.MsgAct;
	import rickyGame.game.scene.Pathfinder;
	import rickyGame.game.skill.SkillData;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	
	/**
	 * 自动攻击ai
	 * 检测可攻击范围类的玩家角色，如果有寻找最近的一个进行攻击
	 */
	public class AutoAttackAi extends MoveLeafAction
	{
		public function AutoAttackAi(actor:Fighter)
		{
			super(actor);
			this._state=0;//AiType.AutoAttackAi;
			this._type="AutoAttackAi";

			_player=actor;
			this.skill=_player.skills.attack1;

			this._radius=Number(_player.data.maxrangeA); //.;//到达半径
			_searchrange=_player.data.rangeVisible/40;//搜索半径
			
		}
		
		override public function begin():void
		{
			_isfind=false;
			_last=new Date().getTime();
			FrameManager.inst.addLogic(getActor())
		}
		
		override public function complete():void
		{
//			JDebug.trace("AutoAttackAi-行动完成!");//
			super.complete();
		}
		
		override public function action(timeline:Number):void
		{
			var dt:Number=timeline-_last;
			if(_isfind==false)
			{
				if(dt>1000)
				{
					_last=timeline;
					findPath();
				}
			}else
				super.action(timeline);
		}
		
		/**
		 * 寻找敌人
		 */
		private function searchEnemy():Fighter
		{
			var g:Array= GameState.inst.sceneManager.getGridAround(_player.gridX,_player.gridY,_searchrange);
			
			var tar:Fighter=null;
			var f:Array=ActUtils.filterTarget(_player,g,skill);
			if(f.length>0)
			{
				tar=f[0];
			}
			
			return tar;
		}
		
		/**
		 * 向目标寻路
		 */
		private function findPath():Boolean
		{
			_target=searchEnemy();
			if(_target)
			{
				var target:Actor2D=_target as Actor2D;
				targetX=target.gridX;
				targetY=target.gridY;
				var tree:Quadtree=GameState.inst.scene.tree;
				var first:Leaf=tree.getLeaf(Actor2D(_actor).morton);
				var destinationNode:Leaf=tree.getLeaf(target.morton);
				var arr:Array=Pathfinder.findPath1(first,destinationNode,tree,Pathfinder.traversableFunction,_actor.asType);
				_path=arr;
				if(_path && _path.length>1)
				{
					this._next=new MsgAct(actor,MsgType.SERVER_ABILITY_ATTACKTARGET,actor,skill.abilityID,null,_target);
					this.addNext(new IdleAi(Actor2D(actor),1000));//休息1s
					moveStep();
					if(this._point)
					{
						_lastTime=new Date().getTime();
						calculateDirection();//super.begin();
					}
					_isfind=true;
					return true;
				}
					
			}
			_isfind=false;
			return false;
		}

		override protected function moveStep():void
		{
			if(_path!=null && _path.length>=1)
			{
				var tree:Quadtree=GameState.inst.scene.tree;
				var me:Leaf=tree.getLeaf(Actor2D(_actor).morton);
				var leaf:Leaf;
				
				if(_target)
				{
					if(targetX!=Actor2D(_target).gridX || targetY!=Actor2D(_target).gridY) //如果目标已经移动了					
					{
						targetX=Actor2D(_target).gridX;
						targetY=Actor2D(_target).gridY;
						_isfind=false;//重新找
						leaf=tree.getLeaf(Actor2D(_target).morton);
						_path=Pathfinder.findPath1(me,leaf,tree,Pathfinder.traversableAroundFunction,_actor.asType);
					}
				}
				
				leaf=_path.shift();//目标位置
				if(!leaf)
				{
					this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
					return;
				}
				
				if(canWalk(leaf))//如果目标可走
				{
					this._point=new Vector3D(leaf.x*Common.tileWidth+20,leaf.y*Common.tileHeight+15);
				}else
				{
					var iswalk:Boolean=false;//=this.canWalk(leaf);
					
					while(!iswalk)//可走
					{
						leaf=_path.shift();
						if(leaf)
							iswalk=this.canWalk(leaf);
						else
							iswalk=true;
					}
					
					if(!leaf)
					{
						this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
						return;
					}
					
					var arr:Array=Pathfinder.findPath(me,leaf,tree,Pathfinder.traversableAroundFunction,_actor.asType);
					if(arr)
					{
						_path=arr.concat(_path);
						
						leaf=_path.shift();
						this._point=new Vector3D(leaf.x*Common.tileWidth+20,leaf.y*Common.tileHeight+15);
					}
				}
				
			}else
				this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
		}
			
		//
		private var _target:Actor;//
		private var _player:Fighter;
		private var skill:SkillData;
		private var _searchrange:int;//索搜半径
		
		private var _isfind:Boolean=false;
		private var targetX:int;
		private var targetY:int;
		
		private var _last:Number;

	}
}